$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'ElectiveCourse/Select',
        datatype: "json",
        colModel: [
            { label: 'ID', name: 'id', width: 40,key: true },
             { label: '课程名称', name: 'courseName', width: 40 },
             { label: '学生学号', name: 'studentId', width: 40 },
             { label: '教师职工号', name: 'teacherId', width: 40 },
             { label: '学生姓名', name: 'studentName', width: 40 },
             { label: '教师姓名', name: 'teacherName', width: 40 },
             { label: '上课时间', name: 'courseTime', width: 40 },
             { label: '是否已评价', name: 'isEvaluation', width: 40 },
             { label: '第几节', name: 'courseWhichIndex', width: 40 },
             { label: '上课地点', name: 'courseClassroom', width: 40 },
             { label: '课程号', name: 'courseId', width: 40 }
         ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList : [10,30,50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        },
        gridComplete:function(){
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });
    new AjaxUpload('#upload', {
            action: baseURL + 'ElectiveCourse/upload?token=' + token,
            name: 'file',
            autoSubmit:true,
            responseType:"json",
            onSubmit:function(file, extension){
                if (!(extension && /^(xls|xlsx)$/.test(extension.toLowerCase()))){
                    alert('只支持excel上传！');
                    return false;
                }
            },
            onComplete : function(file, r){
                if(r.code == 0){
                    alert("上传成功");
                }else{
                    alert(r.msg);
                }
            }
        });
});

var vm = new Vue({
    el:'#rrapp',
    data:{
        q:{
            key: null
        },
        showList: true,
        title: null,
        ElectiveCourse: {},
        id: null,
        uploadCount:null,
        dialogProgressVisible:false,
        webSocket:null
    },
    created(){
        this.initWebSocket()
    },
    methods: {
        initWebSocket:function(){
            this.webSocket=new WebSocket("ws://"+localStorage.getItem("webSocketPort")+"/webSocket?token="+token);
            this.webSocket.onmessage=this.websocketonmessage;
            this.webSocket.onclose = this.websocketclose;
            console.log("建立连接")
        },
        websocketonmessage:function(e){ //数据接收
            console.log(e.data);
            vm.uploadCount=e.data;
            vm.dialogProgressVisible=true;
            if(e.data=="100.0"){
             vm.dialogProgressVisible=false;
            }
        },
        websocketsend(agentData){//数据发送
            this.websock.send(agentData);
        },
        websocketclose(e){  //关闭
            console.log("connection closed (" + e.code + ")");
        },
        query: function () {
            vm.reload();
        },
        add: function(){
            vm.showList = false;
            vm.title = "新增";
            vm.ElectiveCourse = {};
        },
        update: function () {
            var id = getSelectedRow();
            if(id == null){
                return ;
            }
            $.get(baseURL + "ElectiveCourse/Info/"+id, function(r){
                vm.showList = false;
                vm.title = "修改";
                vm.ElectiveCourse = r.ElectiveCourse;
                vm.id = r.ElectiveCourse.id;
            });
        },
        del: function () {
            var ids = getSelectedRows();
            if(ids == null){
                return ;
            }
            confirm('确定要删除选中的记录？', function(){
                $.ajax({
                    type: "POST",
                    url: baseURL + "ElectiveCourse/Delete",
                    contentType: "application/json",
                    data: JSON.stringify(ids),
                    success: function(r){
                        if(r.code == 0){
                            alert('操作成功', function(){
                                vm.reload();
                            });
                        }else{
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        saveOrUpdate: function () {
            if(vm.validator()){
                return ;
            }

            var url = vm.ElectiveCourse.id == vm.id ? "ElectiveCourse/Update" : "ElectiveCourse/Insert";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.ElectiveCourse),
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(){
                            vm.reload();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        reload: function () {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{
                postData:{ 'id': vm.q.key},
                page:page
            }).trigger("reloadGrid");
        },
        validator: function () {
            if(isBlank(vm.ElectiveCourse.id)){
                alert("ID不能为空");
                return true;
            }
            if(isBlank(vm.ElectiveCourse.courseName)){
                alert("课程名称不能为空");
                return true;
            }
            if(isBlank(vm.ElectiveCourse.studentId)){
                alert("学生学号不能为空");
                return true;
            }
            if(isBlank(vm.ElectiveCourse.teacherId)){
                alert("教师职工号不能为空");
                return true;
            }
            if(isBlank(vm.ElectiveCourse.studentName)){
                alert("学生姓名不能为空");
                return true;
            }
            if(isBlank(vm.ElectiveCourse.teacherName)){
                alert("教师姓名不能为空");
                return true;
            }
            if(isBlank(vm.ElectiveCourse.courseTime)){
                alert("上课时间不能为空");
                return true;
            }
            if(isBlank(vm.ElectiveCourse.isEvaluation)){
                alert("是否已评价不能为空");
                return true;
            }
            if(isBlank(vm.ElectiveCourse.courseWhichIndex)){
                alert("第几节不能为空");
                return true;
            }
            if(isBlank(vm.ElectiveCourse.courseClassroom)){
                alert("上课地点不能为空");
                return true;
            }
            if(isBlank(vm.ElectiveCourse.courseId)){
                alert("课程号不能为空");
                return true;
            }
            }
        }
});